/*
 * Created on 2005-3-12
 *
 */
package bma.common.langutil.bean.impl;

import java.util.Collection;
import java.util.List;

import bma.common.langutil.bean.AccessType;
import bma.common.langutil.core.ClassUtil;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 */
public class BeanListNative extends BeanListAbstract {

	public BeanListNative(Class prototype) {
		super(prototype);
	}

	@Override
	public boolean accessible(AccessType type) {
		switch (type) {
		case INDEX_REMOVE:
		case INDEX_SET:
		case INDEX_GET:
			if (prototype != null) {
				return ClassUtil.isImplementInterface(prototype, List.class);
			}
		case SIZE:
		case ADD:
			return true;
		default:
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void add(Object thisObject, Object value) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof Collection)) {
			throw new IllegalArgumentException("Collection.class");
		}
		Collection obj = (Collection) thisObject;
		obj.add(value);
	}

	@Override
	public Object get(Object thisObject, int index) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof List)) {
			throw new IllegalArgumentException("List.class");
		}
		List obj = (List) thisObject;
		return obj.get(index);
	}

	@Override
	public int size(Object thisObject) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof Collection)) {
			throw new IllegalArgumentException("Collection.class");
		}
		Collection obj = (Collection) thisObject;
		return obj.size();
	}

	@Override
	public void remove(Object thisObject, int index) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof Collection)) {
			throw new IllegalArgumentException("Collection.class");
		}
		if (!(thisObject instanceof List)) {
			throw new IllegalArgumentException("List.class");
		}
		List obj = (List) thisObject;
		obj.remove(index);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void set(Object thisObject, int index, Object value) {
		if (thisObject == null) {
			throw new NullPointerException("this object");
		}
		if (!(thisObject instanceof List)) {
			throw new IllegalArgumentException("List.class");
		}
		List obj = (List) thisObject;
		obj.set(index, value);
	}

}
